# -*- coding: utf-8 -*-
import json

from django import forms

from common.forms import BaseComponentForm
from components.component import Component
from .toolkit import configs
from .toolkit.tools import base_api_url


class GetAllBusiness(Component):
    """
    apiMethod GET

    ### 功能描述

    获取所有业务以及实例

    ### 请求参数
    {{ common_args_desc }}

    ### 返回结果示例

    ```python
    {
        "code": 200,
        "apicode": 20001,
        "result": true,
        "request_id": xxxxxxxxxxxxxxxxxxxxxxxx,
        "message": "公有云地区列表获取成功",
        "data": [
        {
            "son": [
                {
                    "code": 11,
                    "model_code": "APPLICATION",
                    "data": {
                        "APPLICATION_name": "商城中台",
                        "APPLICATION_ID": "SHARE_SERVICE",
                        "APPLICATION_STATUS": "运行中",
                        "APPLICATION_IN_BUSINESS": "网上商城"
                    },
                    "pk_name": "APPLICATION_name",
                    "pk_value": "网上商城->商城中台",
                    "position": "yw",
                    "import_type": "逐条录入",
                    "link_inst": [],
                    "parent_inst": "1",
                    "business_code": "未分配"
                }
            ]
        },
        {
            "agent_info": [
                {
                    "normal": 0,
                    "not_install": 1,
                    "agent_info": [
                        {
                            "ip": "xxxxxxxxxxxxxxxx",
                            "agent_state": "Agent未安装",
                            "controller_name": "本地服务器",
                            "system_type": "Linux",
                            "parent_inst": "xxxx",
                            "host_name": "xxxxxxxxxxxxxxx",
                            "ip_type": "外网IP"
                        }
                    ]
                }
            ]
        }
    ]
    }
    ```
    """

    # 组件所属系统的系统名
    sys_name = configs.SYSTEM_NAME

    # Form处理参数校验
    class Form(BaseComponentForm):
        pass

        # clean方法返回的数据可通过组件的form_data属性获取
        def clean(self):
            return self.get_cleaned_data_when_exist()

    # 组件处理入口
    def handle(self):
        # 获取Form clean处理后的数据
        params = self.form_data

        # 设置当前操作者
        params['operator'] = self.current_user.username

        # 请求系统接口
        response = self.outgoing.http_client.get(
            host=configs.host,
            path='{}get-all-business/'.format(base_api_url),
            params=params,
            data=None,
            cookies=self.request.wsgi_request.COOKIES,
        )

        # 对结果进行解析
        code = response['code']
        if code == 200:
            result = {
                'code': response['code'],
                'api_code': response['successcode'],
                'message': response['message'],
                'result': True,
                'data': response['data'],
            }
        else:
            result = {
                'api_code': response['errcode'],
                'result': False,
                'message': response['message']
            }

        # 设置组件返回结果，payload为组件实际返回结果
        self.response.payload = result
